package com.chengqj.observer.listener.event;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: pattern
 * @description: 事件监听器
 * @author: chengqj
 * @create: 2018-08-03 10:44
 **/
public class EventListener {
    private Map<Enum, Event> events = new HashMap<>();

    public void addListener(Enum eventType, Object targe, Method method) {
        events.put(eventType, new Event(targe, method));
    }

    public void trigger(Enum eventType) {
        if (events.containsKey(eventType)) {
            Event event = events.get(eventType);
            event.setTrgger(eventType.toString());
            try {
                event.getMethod().invoke(event.getTarge());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
